using Microsoft.AspNetCore.Mvc;

namespace WinAutoUpdate.WebApi.Controllers {
  [ApiController]
  [Route("[controller]")]
  public class UpdateController : ControllerBase {

    // 模拟服务器端版本信息
    private static readonly Dictionary<string, string> fVersionPatchMap = new Dictionary<string, string>
    {
            { "1.0.1", "http://localhost:5056/download/patches/1.0.1_patch.zip" },
            { "1.0.2", "http://localhost:5056/download/patches/1.0.2_patch.zip" }
    };

    /// <summary>
    /// 获取相应版本的补丁下载地址
    /// </summary>
    /// <param name="aVersion"></param>
    /// <returns></returns>
    [HttpGet("patch/{aVersion}")]
    public IActionResult GetPatchForVersion(string aVersion) {
      // TODO versionPatchMap 需要修改成读取数据库方式
      if (fVersionPatchMap.TryGetValue(aVersion, out string? bPatchUrl) && bPatchUrl != null) {
        return Ok(bPatchUrl);
      }
      return NotFound();
    }

    /// <summary>
    /// 获取所有版本信息
    /// </summary>
    /// <returns></returns>
    [HttpGet("versions")]
    public IActionResult GetVersions() {
      // TODO 需要修改成读取数据库方式
      // 这里可以从数据库、配置文件或者其他数据源获取版本号列表
      // 为了示例，我们使用一个硬编码的版本号列表
      var bVersions = new List<string>
      {
                "1.0.0",
                "1.0.1",
                "1.0.2"
      };
      return Ok(bVersions);
    }
  }
}
